package com.jiawa.train.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiawa.train.business.domain.TrainCarriage;
import com.jiawa.train.business.mapper.TrainCarriageMapper;
import com.jiawa.train.business.req.TrainCarriageQueryReq;
import com.jiawa.train.business.req.TrainCarriageSaveReq;
import com.jiawa.train.business.resp.TrainCarriageQueryResp;
import com.jiawa.train.business.service.TrainCarriageService;
import com.jiawa.train.common.resp.PageResp;
import com.jiawa.train.common.util.SnowUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainCarriageServiceImpl extends ServiceImpl<TrainCarriageMapper, TrainCarriage> implements TrainCarriageService {
    private static final Logger LOG = LoggerFactory.getLogger(TrainCarriageServiceImpl.class);

    public void save(TrainCarriageSaveReq req) {
        DateTime now = DateTime.now();
        TrainCarriage trainCarriage = BeanUtil.copyProperties(req, TrainCarriage.class);
        if (ObjectUtil.isNull(trainCarriage.getId())) {
            trainCarriage.setId(SnowUtil.getSnowflakeNextId());
            trainCarriage.setCreateTime(now);
            trainCarriage.setUpdateTime(now);
            this.save(trainCarriage);
        } else {
            trainCarriage.setUpdateTime(now);
            this.updateById(trainCarriage);
        }
    }

    public PageResp<TrainCarriageQueryResp> queryList(TrainCarriageQueryReq req) {
        LambdaQueryWrapper<TrainCarriage> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(TrainCarriage::getTrainCode)
                .orderByAsc(TrainCarriage::getIndex);
        if (ObjectUtil.isNotEmpty(req.getTrainCode())) {
            wrapper.eq(TrainCarriage::getTrainCode, req.getTrainCode());
        }
        LOG.info("查询页码：{}", req.getPage());
        LOG.info("每页条数：{}", req.getSize());
        Page<TrainCarriage> page = new Page<>(req.getPage(), req.getSize());
        Page<TrainCarriage> trainCarriagePage = this.page(page, wrapper);
        LOG.info("总行数：{}", trainCarriagePage.getTotal());
        LOG.info("总页数：{}", trainCarriagePage.getPages());
        List<TrainCarriageQueryResp> list = BeanUtil.copyToList(trainCarriagePage.getRecords(), TrainCarriageQueryResp.class);
        PageResp<TrainCarriageQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(trainCarriagePage.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    public void delete(Long id) {
        this.removeById(id);
    }

    public List<TrainCarriage> selectByTrainCode(String trainCode) {
        LambdaQueryWrapper<TrainCarriage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TrainCarriage::getTrainCode, trainCode)
                .orderByAsc(TrainCarriage::getIndex);
        return this.list(wrapper);
    }
} 